package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Transactional
public class AppointmentServiceImpl extends
        ServiceImpl<AppointmentMapper,Appointment> implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        IPage<Appointment> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_status",0);
        if (!StringUtils.isEmpty(qo.getCustomerName())){
            wrapper.like("customer_name",qo.getCustomerName());
        }
        if (!StringUtils.isEmpty(qo.getCustomerPhone())){
            wrapper.like("customer_phone",qo.getCustomerName());
        }
        if (!StringUtils.isNull(qo.getStatus())){
            wrapper.eq("status",qo.getStatus());
        }
        return new TablePageInfo<Appointment>(super.page(page,wrapper));
    }@Override
    public void arrival(Long id) {
        Appointment appointment = appointmentMapper.selectById(id);
        //预约中才能选择到店
        boolean status = Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus());
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
            wrapper.set(status,"status",Appointment.STATUS_ARRIVAL);
            wrapper.set(status,"actual_arrival_time",new Date());
            wrapper.eq("id",appointment.getId());
            appointmentMapper.update(appointment,wrapper);

    }
    @Override
    public void cancel(Long id) {
        Appointment appointment = appointmentMapper.selectById(id);
        //预约中才能选择取消
        boolean status = Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus());
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.set(status,"status",Appointment.STATUS_CANCEL);
        wrapper.eq("id",appointment.getId());
        appointmentMapper.update(appointment,wrapper);
    }

    @Override
    public void SoftDelete(Long id) {
        Appointment appointment = appointmentMapper.selectById(id);
        //预约中才能选择软删除
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("没预约不能删除");
        }
        boolean status = Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus());
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.set(status,"delete_status",Appointment.DELETE_STATUS_YES);
        wrapper.eq("id",appointment.getId());
        appointmentMapper.update(appointment,wrapper);

    }

    @Override
    public void settlement(Long id) {
        //结算单生成
        Appointment appointment = appointmentMapper.selectById(id);
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.set("status",Appointment.STATUS_SETTLE);
        wrapper.eq("id",appointment.getId());
        appointmentMapper.update(appointment,wrapper);
    }

    @Override
    public Long generateStatement(Long appointmentId) {
        Appointment appointment = appointmentMapper.selectById(appointmentId);
        Statement statementS = statementMapper.selectById(appointmentId);
        if (statementS==null){
            Statement statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType());
            statement.setAppointmentId(appointment.getId());
            statement.setStatus(Statement.STATUS_CONSUME);
            statementMapper.insert(statement);
            this.settlement(appointment.getId());
            return statement.getId();
        }
        return statementS.getId();
    }
}
